package com.Thread;

/**
 * 4个线程输出1到15，被3整除输出a，被5整除输出b，被3和5整除输出ab
 * @author Weseave
 */
public class TestThread implements Runnable{	

    private int n;

    public void setN(int n) {
        this.n = n;
    }

    private static int i=1;

    @Override
    public void run() {
    	
    		while(true){
    			synchronized(this){
    				if(i > n){
    					break;
    				}
	    			if(i % 3==0 && i % 5 == 0){
	                    System.out.print("ab ");
	                }else if(i % 3 == 0){
	                    System.out.print("a ");
	                }else if(i % 5 == 0){
	                    System.out.print("b ");
	                }else{
	                    System.out.print(i +" ");
	                }
	    			i = i+1;
    			}
//        System.out.println();
    		}
    }

    public static void main(String[] args) {
        int n = 15;
        TestThread topic01 = new TestThread();
        topic01.setN(n);
        Thread thread1 = new Thread(topic01);
        Thread thread2 = new Thread(topic01);
        Thread thread3 = new Thread(topic01);
        Thread thread4 = new Thread(topic01);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

    }
}
